/**
 * Created by bshao on 12/20/15.
 */


import org.apache.commons.codec.binary.Base64
import org.apache.commons.io.IOUtils

import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream

def input = """\
wlqq://QrPay/{"amount":"5","sellerDomainId":1,"sellerName":"昆兴汽配","remark":"测试","description":"[停车] 测试","sellerId":775598,"productType":1}
"""

println input.getBytes("utf-8").length

def deflate(String input) {
    def sis = IOUtils.toInputStream(input, "utf-8")

    def bos = new ByteArrayOutputStream()
    def gos = new GZIPOutputStream(bos)

    IOUtils.copy(sis, gos)

    gos.finish()
    gos.flush()
    gos.close()

    def bytes = bos.toByteArray();

    println("inflate, bytes: ${bytes.length}")

    Base64.encodeBase64String(bytes)
}

def inflate(String input) {
    byte[] bytes = Base64.decodeBase64(input)

    println("inflate, bytes: ${bytes}")

    def gis = new GZIPInputStream(new ByteArrayInputStream(bytes))
    def bos = new ByteArrayOutputStream()
    IOUtils.copy(gis, bos)
    def inflatedBytes = bos.toByteArray()
    new String(inflatedBytes, "utf-8")
}

println("原始文本: ${input}")

def deflatedText = deflate(input)

println("压缩后：${deflatedText.length()}")

def inflatedText = inflate(deflatedText)

println("解压后：${inflatedText}")



